package com.zjj.lbw.microservice.nacos.consumer.nacosconsumer.loadBalancer;

import com.zjj.lbw.common.bean.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * @author zhanglei.zjj
 * @description WebClient使用MVC层接口
 * @date 2023/7/6 11:30
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class WebClientMVC {
    @Autowired
    private WebClient webClient;

    @RequestMapping(value = "/findOrderByUserId2/{id}")
    public Mono<R> findOrderByUserIdWithWebClient(@PathVariable("id") Integer id) {

        String url = "http://nacos-provider/order/findOrderByUserId/" + id;
        //基于WebClient
        Mono<R> result = webClient.get().uri(url)
                .retrieve().bodyToMono(R.class);
        return result;
    }

    @Autowired
    private ReactorLoadBalancerExchangeFilterFunction lbFunction;

    @RequestMapping(value = "/findOrderByUserId3/{id}")
    public Mono<R> findOrderByUserIdWithWebFlux(@PathVariable("id") Integer id) {

        String url = "http://nacos-provider/order/findOrderByUserId/" + id;
        //基于WebClient+webFlux
        Mono<R> result = WebClient.builder()
                .filter(lbFunction)
                .build()
                .get()
                .uri(url)
                .retrieve()
                .bodyToMono(R.class);
        return result;
    }
}
